﻿using System;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls;

using vtech_formBase;
using vtech_frameWorks.Exceptions;

namespace vtech_system.Department
{
    public partial class frmDepartment : frmListWithGrid
    {
        public frmDepartment()
        {
            InitializeComponent();
        }

        #region LoadData
        protected override void LoadData()
        {
            dsDepartment.Tables["DtDepartment"].Clear();
            dsDepartment.Tables["DtDepartment"].Merge(ClsDepartmentBS.GetAllDepartment());
        }

        private void frmDepartment_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region Add
        protected override void Add()
        {
            using (frmAddDepartment fAddDepartment = new frmAddDepartment())
            {
                fAddDepartment.OnAddDelegate += LoadData;
                fAddDepartment.ShowDialog();
            }
        }
        #endregion

        #region Edit
        protected override void Edit()
        {
            ID = (Guid)gridDepartment.SelectedRows[0].Cells[0].Value;
            using (frmEditDepartment fEditDepartment = new frmEditDepartment(ID))
            {
                fEditDepartment.OnEdit += LoadData;
                fEditDepartment.ShowDialog();
            }
        }

        private void gridDepartment_DoubleClick(object sender, EventArgs e)
        {
            Edit();
        }
        #endregion

        #region Reload
        protected override void Reload()
        {
            LoadData();
        }
        #endregion

        protected override void Printer()
        {
            using (frmPrintView fPrintView = new frmPrintView(dsDepartment, @"vtech_system\Department\RptDepartment.rpt", "Danh sách phòng ban"))
            {
                fPrintView.ShowDialog();
            }
            //base.Printer();
        }

        #region Delete
        protected override void Delete()
        {
            try
            {
                if (gridDepartment.SelectedRows.Count != 0)
                {
                    ID = new Guid(gridDepartment.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == RadMessageBox.Show("Bạn muốn xóa phòng ban này ?", "Thông báo", MessageBoxButtons.YesNo, RadMessageIcon.Question))
                    {
                        DataRow[] drow;
                        drow = ClsDepartmentBS.GetAllDepartment().Select(String.Format("DepartmentID = '{0}'", ID));
                        foreach (DataRow row in drow)
                        {
                            ClsDepartmentBS.DoDelete(row);
                        }
                        LoadData();
                    }
                    else
                    { return; }
                }
                else
                {
                    ClsInformation.Error("Chưa chọn bản ghi.Không thể xóa!");
                    return;
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }
        #endregion

        
        
    }
}
